﻿using ArchivingDataModel;
using ArchivingDataWebForms.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ArchivingDataWebForms.Pages.Login
{
    public partial class WebFormCreateUser : WebFormBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void CreateUserWizard_CreatingUser(object sender, LoginCancelEventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(this.Request.QueryString["Employee_ID"]))
                {
                    long id = Convert.ToInt64(this.Request.QueryString["Employee_ID"]);
                    using (ArchivingDataEntities ent = new ArchivingDataEntities())
                    {
                        var employee = ent.Employees.FirstOrDefault(i => i.Employee_ID == id);
                        this.CreateUserWizard.Email = employee.Email;
                    }
                }
            }
            catch (Exception er)
            {
                this.ShowNotification("حدث الخطا التالي اثناء الحفظ " + er.Message, NotificationIconType.Error);
                return;
            }
        }
        protected void CreateUserWizard_CreatedUser(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(this.Request.QueryString["Employee_ID"]))
                {
                    long id=Convert.ToInt64(this.Request.QueryString["Employee_ID"]);
                    using (ArchivingDataEntities ent = new ArchivingDataEntities())
                    {
                        var employee = ent.Employees.FirstOrDefault(i => i.Employee_ID == id);
                        employee.User_ID = Membership.GetUser(this.CreateUserWizard.UserName).ProviderUserKey.ToString();
                        ent.SaveChanges();
                        Response.Redirect("~/Pages/Employees/ViewEmployees.aspx");
                    }
                }
            }
            catch(Exception er)
            {
                this.ShowNotification("حدث الخطا التالي اثناء الحفظ " + er.Message, NotificationIconType.Error);
                return;
            }
        }

    }
}